/*
 * Project: BooEditor
 * Author:  Johannes Kuhaupt
 * Copyright (c): Johannes Kuhaupt
 * Licence: MIT
 */
package view;

import control.FileManager;
import control.GuiManager;
import control.boo.AbstractBooTask;
import control.boo.BooCompiler;
import control.boo.BooInterpreter;
import java.awt.BorderLayout;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import javax.swing.JPanel;
import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
import org.fife.ui.rtextarea.RTextScrollPane;

/**
 *
 * @author Jojo
 */
public class CustomTabPanel extends JPanel{
    BooTextEditorPane textEditor;
    RTextScrollPane scroller;
    FileManager fileMan;
    AbstractBooTask boo;
    java.util.ResourceBundle langBundle = GuiManager.getLangBundle();
    
    public CustomTabPanel(File pFile){
        super();
        this.setLayout(new BorderLayout());
        
        AbstractTokenMakerFactory atmf = (AbstractTokenMakerFactory) TokenMakerFactory.getDefaultInstance();
        atmf.putMapping("text/boo", "BooSyntax.BooTokenManager");
        TokenMakerFactory.setDefaultInstance(atmf);
        
        textEditor = new BooTextEditorPane();
        scroller = new RTextScrollPane(this.textEditor);
        this.textEditor.setScroller(scroller);
        this.add(scroller, BorderLayout.CENTER);
        this.configTextEditor();
        this.updateGuiEditor();
        this.fileMan=new FileManager(this.textEditor, pFile);        
        GuiManager.addEditorToList(textEditor);        
        textEditor.requestFocusInWindow(); 
    }
    
    private void configTextEditor(){
        //Scrollpane + Zeilenummern        
        this.textEditor.setAutoscrolls(true);
        scroller.setIconRowHeaderEnabled(false); //keine Auswirkungen
        scroller.setAutoscrolls(true);
        scroller.getGutter().setBookmarkingEnabled(false);
        this.textEditor.setStandardStyle();
        
        //this.textEditorPane.requestFocus();
        //this.textEditorPane.requestFocusInWindow();
    }
    
    public boolean isTextDirty(){
        return (this.textEditor.isDirty());
    }
    
    public boolean isDocumentEmpty(){
        return !(this.textEditor.getDocument().getLength()>0);
    }
    
    public void loadFileInEditor(File pFile) throws IOException{
        this.textEditor.loadAndShowFile(pFile);
    }
    
    
    private void updateGuiEditor(){
        this.textEditor.updateOptions();
    }


    void handleSaveFileAction() {
        this.fileMan.handleSaveFileAction();
    }

    void handleCloseFileAction() {
        this.fileMan.handleCloseFileAction();
    }

    void handleOpenFileAction() {
        this.fileMan.handleOpenFileAction();
    }
    
    void handleSaveFileAs(){
        this.fileMan.saveFileAs();
    }
    
    void handleNewFileAction(){
        this.fileMan.handleNewFileAction();
    }
    
    private void startBooInterpreting(){
        boo = new BooInterpreter(this.textEditor.getFileFullPath());
        boo.start();
        GuiManager.getInstance().showOutputTab();
    }
    
    private void startBooCompiling(boolean compileToDll){
        boo = new BooCompiler(this.textEditor.getFileFullPath(), compileToDll);
        boo.start();
        GuiManager.getInstance().showOutputTab();
    }
    
    boolean prepareBooJob(){
        if(this.isSavedFile()){            
            if(this.isTextDirty()){
                this.handleSaveFileAction(); 
                //this.startBooInterpreting();
                return true;
            }
            else{
                if(this.isDocumentEmpty()){
                    GuiManager.getInstance().showMessage(langBundle.getString("MsgInfoEmptyDocument"));
                    return false;
                }
                else{
                   //this.startBooInterpreting();
                    return true;
                }
                
            }            
        }
        else{
            this.handleSaveFileAction();
            return false;
        }
    }
    

    void startBooInterpreter() {
        if(this.prepareBooJob()){
            startBooInterpreting();
        }               
    }
    
    void startBooCompiler(boolean pDll){
        if(this.prepareBooJob()){
            startBooCompiling(pDll);
        }
    }

    void stopBooTask() {
        boo.stopBooTask();
        boo = null;
    }

    void handleCut() {
        this.textEditor.cut();
    }

    void handleCopy() {
        this.textEditor.copy();
    }

    void handlePaste() {
        this.textEditor.paste();
    }

    boolean search(String pText, boolean b) {
        return this.textEditor.search(pText, b);
    }

    void handlePrint() throws PrinterException {
        this.textEditor.print();
    }

    boolean handleCanUndo() {
        return this.textEditor.canUndo();
    }

    boolean handleCanRedo() {
        return this.textEditor.canRedo();
    }

    void handleUndoLastAction() {
        if(this.textEditor.canUndo()){
        this.textEditor.undoLastAction();
    }
    }

    void handleRedoLastAction() {
        if(this.textEditor.canRedo()){
        this.textEditor.redoLastAction();
    }
    }
    
    void handleSelectAll(){
        this.textEditor.selectAll();
    }
    
    boolean isSavedFile(){
        return this.textEditor.isLocalAndExists();
    }
    
    String getFilePath(){
        return this.textEditor.getFileFullPath();
    }
    
    
   

    public void setFocusToEditor() {
        this.textEditor.requestFocus(true);
    }
    
    
    
    
    
}
